<?php
/**
 * User: hjd-yao
 * Date: 2018/12/19
 * Time: 11:00
 */

namespace App\Http\Controllers\Backend;


use App\Helpers\SelfResponse;
use App\Http\Controllers\BackendController;
use App\Models\System\AuthModel;
use App\Models\System\MenuModel;
use Illuminate\Http\Request;

class MenuController extends BackendController
{
    private static $_menuModel;
    public function __construct()
    {
        self::$_menuModel = new MenuModel();
    }

    /**
     * @title 菜单管理
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function showAllMenus()
    {
        $list = self::$_menuModel->whereIn('status',[0,1])->orderBy('sort')->get()->toArray();
        $list = list_to_tree($list,true,'id','parent_id');
        return view('backend::menu.menu_list',[
            'page_title'=>'菜单管理',
            'list'=>$list
        ]);
    }

    /**
     * @title 添加菜单
     * @param Request $request
     * @param int $parent_id
     * @return SelfResponse|\Illuminate\View\View
     */
    public function addMenus(Request $request,int $parent_id=0)
    {
        if($request->isMethod('post')){
            $data = $request->post();
            $res = self::$_menuModel->addMenu([
                'title'=>$data['menu_title'],
                'url'=>$data['route_name'],
                'icon'=>$data['menu_icon'],
                'parent_ids'=>$data['menu_parents'],
                'sort'=>$data['sort']??1000,
                'status'=>$data['status'],
                'comment'=>$data['comment'],
            ]);

            if($res){
                return SelfResponse::success('添加成功','b_Menu_showAllMenus');
            }
            return SelfResponse::error('添加失败');
        }else{
            $has_url = self::$_menuModel->pluck('url')->toArray();
            $actions = AuthModel::where('status',1)->whereIn('auth_type',[1,2])->whereNotIn('action',$has_url)->pluck('title','action');
            if($parent_id){
                $parent_ids = self::$_menuModel->where('id',$parent_id)->value('parent_ids');
                $parent_ids = $parent_ids?$parent_ids.','.$parent_id:$parent_id;
            }else{
                $parent_ids = $parent_id;
            }

            return view('backend::menu.add_menu',[
                'page_title'=>'添加菜单',
                'actions'=>$actions,
                'action'=>route('b_Menu_addMenus',['parent_id'=>$parent_id]),
                'parent_ids'=>$parent_ids,
                'parent_id'=>$parent_id,
                'parent_menus'=>json_encode(MenuModel::init()->getTreesMenu())
            ]);
        }
    }

    /**
     * @title 编辑菜单
     * @param Request $request
     * @param $menu_id
     * @return SelfResponse|\Illuminate\View\View
     */
    public function editMenu(Request $request,int $menu_id)
    {
        $menu = MenuModel::find($menu_id);
        if($request->isMethod('post')){
            $data = $request->post();
            if(self::$_menuModel->editMenu($data,$menu_id)){
                return SelfResponse::success('更新成功','b_Menu_showAllMenus');
            }
            return SelfResponse::error(self::$_menuModel->getErrorMsg()?:'更新失败');
        }else{
            return view('backend::menu.edit_menu',[
                'page_title'=>'编辑'.$menu->title,
                'action'=>route('b_Menu_editMenu',['menu_id'=>$menu_id]),
                'menu'=>$menu,
                'parent_ids'=>$menu->parent_ids,
                'parent_menus'=>json_encode(MenuModel::init()->getTreesMenu())
            ]);
        }
    }

    /**
     * @title 删除菜单
     * @param int $menu_id
     * @return SelfResponse|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
     */
    public function deleteMenus(int $menu_id)
    {
        $menu = MenuModel::find($menu_id);
        if(!$menu){
            return SelfResponse::error('操作菜单不存在');
        }
        if(self::$_menuModel->where('id',$menu_id)->delete()){
            self::$_menuModel->whereRaw('FIND_IN_SET(?,`parent_ids`)',[$menu_id])->delete();
            return SelfResponse::success('删除成功');
        }
        return SelfResponse::error('删除失败');
    }

    /**
     * @title 启用/禁用菜单
     * @param $menu_id
     * @return SelfResponse|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
     */
    public function switchMenu(int $menu_id)
    {
        $menu = MenuModel::find($menu_id);
        if(!$menu){
            return SelfResponse::error('操作菜单不存在');
        }
        $status = abs($menu->status-1);
        $menu->status = $status;
        $menu->updated_at = time();
        if($menu->save()){
            return SelfResponse::success('ok');
        }
        return SelfResponse::error('操作失败');
    }
}